using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Serialization;
using ns22;
using Triton.Common;

namespace Triton.Bot.Profiles
{
	public class XmlSerializerFactory
	{
		public static XmlSerializer GetSerializerForCurrentDomain<T, TProfileTagType>()
		{
			XmlAttributeOverrides xmlAttributeOverrides = new XmlAttributeOverrides();
			smethod_0<TProfileTagType>(xmlAttributeOverrides);
			return new XmlSerializer(typeof(T), xmlAttributeOverrides, null, new XmlRootAttribute("Profile"), "");
		}

		private static void smethod_0<T>(XmlAttributeOverrides xmlAttributeOverrides_0)
		{
			Dictionary<Type, TypeFinder> dictionary = new Dictionary<Type, TypeFinder>();
			foreach (Tuple<MemberInfo, List<Attribute37>> attributedMember in new AttributeFinder<Attribute37>().AttributedMembers)
			{
				foreach (Attribute37 item in attributedMember.Item2)
				{
					if (!dictionary.TryGetValue(item.Type_0, out var value))
					{
						value = new TypeFinder(item.Type_0);
					}
					smethod_1(xmlAttributeOverrides_0, attributedMember.Item1.DeclaringType, attributedMember.Item1.Name, value.InstanceTypes);
				}
			}
		}

		private static void smethod_1(XmlAttributeOverrides xmlAttributeOverrides_0, Type type_0, string string_0, IEnumerable<Type> ienumerable_0)
		{
			XmlAttributes xmlAttributes = new XmlAttributes();
			MemberInfo[] members = type_0.GetMembers();
			foreach (MemberInfo memberInfo in members)
			{
				if (memberInfo.Name == string_0)
				{
					object[] customAttributes = memberInfo.GetCustomAttributes(typeof(XmlElementAttribute), inherit: false);
					for (int j = 0; j < customAttributes.Length; j++)
					{
						XmlElementAttribute attribute = (XmlElementAttribute)customAttributes[j];
						xmlAttributes.XmlElements.Add(attribute);
					}
				}
			}
			foreach (Type item in ienumerable_0)
			{
				xmlAttributes.XmlElements.Add(new XmlElementAttribute(item.Name, item));
			}
			xmlAttributeOverrides_0.Add(type_0, string_0, xmlAttributes);
		}
	}
}
